package ADBook.Advance;

import PhotovoltaicPowerStation.Manager.Manager;

import java.awt.Component;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;



public class ADB_SYS {
	//declaration
	public UI ADB;
	ADB_SYS(){
		ADB = new UI("通讯录管理系统");
		Manager contact_manager = new Manager();
		
		//complete the add function
		ADB.add_b.addActionListener(e->{
			//1. get the input
			String name = ADB.name_input.getText();
			String phone_num = ADB.phone_input.getText();
			String sex = null;
			for(Component c : ADB.panel.getComponents()) {
				if (c instanceof JRadioButton) {
					if (((JRadioButton) c).isSelected()) {
						sex = ((JRadioButton) c).getText();
					}
				}
			}
			//2. connect the database
			if (name.equals("")||phone_num.equals("")||sex == null) {
				System.out.print(sex);
				JOptionPane.showMessageDialog(null, "不能有内容为空");
			} else {  //3. do the function
				//connect
				try {
					ADB.conn = DriverManager.getConnection(ADB.URL, ADB.USERNAME, ADB.PASSWORD);
					//statement
					ADB.stmt = ADB.conn.createStatement();
					//add data into datebase
					String sql = "insert into adb values('"+ name + "','" + sex +"','" + phone_num +"')";
					ADB.stmt.executeUpdate(sql);
					//update the data in the UI
					Vector row = new Vector();
					row.add(name);
					row.add(sex);
					row.add(phone_num);
					ADB.infotable_model.addRow(row);
					//clear the textfield
					ADB.name_input.setText("");
					ADB.phone_input.setText("");
					//to notify the info has been added successfully
                    JOptionPane.showMessageDialog(null, "添加成功");
					//close the connection
					ADB.stmt.close();
					ADB.conn.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			
		});
		//delete
		ADB.delete_b.addActionListener(e->{
			
			int[] selectedRows = ADB.infotable.getSelectedRows();//被选中行的索引集合
			if ( selectedRows.length != 0) {    //如果被选中行不为0
				for(int i = selectedRows.length-1; i >= 0; i--){//这是关键代码
					try {
						String name = ADB.infotable.getValueAt(i, 0).toString();
						String phonenum = ADB.infotable.getValueAt(i, 2).toString();
							//创建数据库连接
			                ADB.conn = DriverManager.getConnection(ADB.URL, ADB.USERNAME, ADB.PASSWORD);
			                //创建数据库查询语句
			                ADB.stmt = ADB.conn.createStatement();
			                //删除数据库中的数据
			                String sql = "delete from adb where name='" + name + "' and phone_num='" + phonenum + "'";
			                ADB.stmt.executeUpdate(sql);
			                //删除表格中的数据
			                int row = ADB.infotable.getSelectedRow();
			                ADB.infotable_model.removeRow(row);
			                //清空输入框
			                ADB.name_input.setText("");
			                ADB.phone_input.setText("");
			                //成功删除
			                JOptionPane.showMessageDialog(null,"删除成功");
			                //关闭数据库连接
			                ADB.stmt.close();
			                ADB.conn.close();
			                
					}catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			}
		});
		//update
		ADB.update_b.addActionListener(e->{
			//获取用户输入的姓名和电话号码和类型
            String name = ADB.name_input.getText();
            String phonenum = ADB.phone_input.getText();
            //连接数据库找到相关数据并修改
            try {
                //创建数据库连接
                ADB.conn = DriverManager.getConnection(ADB.URL, ADB.USERNAME, ADB.PASSWORD);
                //创建数据库查询语句
                ADB.stmt = ADB.conn.createStatement();
                //修改数据库中的数据
                String sql = "update adb set phone_num='" + phonenum + "' where name='" + name + "'";
                ADB.stmt.executeUpdate(sql);
                //修改表格中的数据
                int row = ADB.infotable.getSelectedRow();
                ADB.infotable_model.setValueAt(phonenum, row, 2);
                //清空输入框
                ADB.name_input.setText("");
                ADB.phone_input.setText("");
                //关闭数据库连接
                ADB.stmt.close();
                ADB.conn.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
		});
		//query
		ADB.query_b.addActionListener(e->{
			//获取用户输入的姓名
            String name = ADB.name_input.getText();
            //连接数据库找到相关数据并显示
            try {
                //创建数据库连接
                ADB.conn = DriverManager.getConnection(ADB.URL, ADB.USERNAME, ADB.PASSWORD);
                //创建数据库查询语句
                ADB.stmt = ADB.conn.createStatement();
                //查询数据库中的数据
                String sql = "select * from adb where name='" + name + "'";
                ADB.rest = ADB.stmt.executeQuery(sql);
                //显示表格中的数据
                while (ADB.rest.next()) {
                    Vector row = new Vector();
                    row.add(ADB.rest.getString(1));
                    row.add(ADB.rest.getString(2));
                    row.add(ADB.rest.getString(3));
                    ADB.infotable_model.addRow(row);
                }
                //清空输入框
                ADB.name_input.setText("");
                ADB.phone_input.setText("");
                //提示查询成功
                JOptionPane.showConfirmDialog(null, "查询成功!");
                //关闭数据库连接
                ADB.stmt.close();
                ADB.conn.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
		});
	}
	
	public static void main(String args[]) {
		new ADB_SYS();
	}
}
